/*     */ package com.cr.framework.aspectj;
/*     */ 
/*     */ import com.cr.common.annotation.DataScope;
/*     */ import com.cr.common.core.domain.BaseEntity;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import java.lang.reflect.Method;
/*     */ import org.aspectj.lang.JoinPoint;
/*     */ import org.aspectj.lang.Signature;
/*     */ import org.aspectj.lang.annotation.Aspect;
/*     */ import org.aspectj.lang.annotation.Before;
/*     */ import org.aspectj.lang.annotation.Pointcut;
/*     */ import org.aspectj.lang.reflect.MethodSignature;
/*     */ import org.springframework.stereotype.Component;
/*     */ 
/*     */ @Aspect
/*     */ @Component
/*     */ public class DataScopeAspect {
/*     */   public static final String DATA_SCOPE_ALL = "1";
/*     */   
/*     */   public static final String DATA_SCOPE_CUSTOM = "2";
/*     */   
/*     */   public static final String DATA_SCOPE_DEPT = "3";
/*     */   
/*     */   public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
/*     */   
/*     */   public static final String DATA_SCOPE_SELF = "5";
/*     */   
/*     */   public static final String DATA_SCOPE = "dataScope";
/*     */   
/*     */   @Pointcut("@annotation(com.cr.common.annotation.DataScope)")
/*     */   public void dataScopePointCut() {}
/*     */   
/*     */   @Before("dataScopePointCut()")
/*     */   public void doBefore(JoinPoint point) throws Throwable {
/*  66 */     clearDataScope(point);
/*  67 */     handleDataScope(point);
/*     */   }
/*     */   
/*     */   protected void handleDataScope(JoinPoint joinPoint) {
/*  73 */     DataScope controllerDataScope = getAnnotationLog(joinPoint);
/*  74 */     if (controllerDataScope == null)
/*     */       return; 
/*  79 */     SysUser currentUser = ShiroUtils.getSysUser();
/*  80 */     if (currentUser != null)
/*  83 */       if (!currentUser.isAdmin())
/*  85 */         dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(), controllerDataScope
/*  86 */             .userAlias());  
/*     */   }
/*     */   
/*     */   public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias) {
/* 101 */     StringBuilder sqlString = new StringBuilder();
/* 103 */     for (SysRole role : user.getRoles()) {
/* 105 */       String dataScope = role.getDataScope();
/* 106 */       if ("1".equals(dataScope)) {
/* 108 */         sqlString = new StringBuilder();
/*     */         break;
/*     */       } 
/* 111 */       if ("2".equals(dataScope)) {
/* 113 */         sqlString.append(MyStringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", new Object[] { deptAlias, role
/*     */                 
/* 115 */                 .getRoleId() }));
/*     */         continue;
/*     */       } 
/* 117 */       if ("3".equals(dataScope)) {
/* 119 */         sqlString.append(MyStringUtils.format(" OR {}.dept_id = {} ", new Object[] { deptAlias, user.getDeptId() }));
/*     */         continue;
/*     */       } 
/* 121 */       if ("4".equals(dataScope)) {
/* 123 */         sqlString.append(MyStringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", new Object[] { deptAlias, user
/*     */                 
/* 125 */                 .getDeptId(), user.getDeptId() }));
/*     */         continue;
/*     */       } 
/* 127 */       if ("5".equals(dataScope)) {
/* 129 */         if (MyStringUtils.isNotBlank(userAlias)) {
/* 131 */           sqlString.append(MyStringUtils.format(" OR {}.user_id = {} ", new Object[] { userAlias, user.getUserId() }));
/*     */           continue;
/*     */         } 
/* 136 */         sqlString.append(" OR 1=0 ");
/*     */       } 
/*     */     } 
/* 141 */     if (MyStringUtils.isNotBlank(sqlString.toString())) {
/* 143 */       Object params = joinPoint.getArgs()[0];
/* 144 */       if (MyStringUtils.isNotNull(params) && params instanceof BaseEntity) {
/* 146 */         BaseEntity baseEntity = (BaseEntity)params;
/* 147 */         baseEntity.getParams().put("dataScope", " AND (" + sqlString.substring(4) + ")");
/*     */       } 
/*     */     } 
/*     */   }
/*     */   
/*     */   private DataScope getAnnotationLog(JoinPoint joinPoint) {
/* 157 */     Signature signature = joinPoint.getSignature();
/* 158 */     MethodSignature methodSignature = (MethodSignature)signature;
/* 159 */     Method method = methodSignature.getMethod();
/* 161 */     if (method != null)
/* 163 */       return method.<DataScope>getAnnotation(DataScope.class); 
/* 165 */     return null;
/*     */   }
/*     */   
/*     */   private void clearDataScope(JoinPoint joinPoint) {
/* 173 */     Object params = joinPoint.getArgs()[0];
/* 174 */     if (MyStringUtils.isNotNull(params) && params instanceof BaseEntity) {
/* 176 */       BaseEntity baseEntity = (BaseEntity)params;
/* 177 */       baseEntity.getParams().put("dataScope", "");
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\aspectj\DataScopeAspect.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */